//
//  RegistViewController.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "RegistViewController.h"
#import "LoginViewController.h"

@interface RegistViewController ()


@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)registerDidClicked:(id)sender {
//    NSString *username = [_username text];
//    NSString *password = [_password text];
//    NSString *confirmPassword = [_confirmPassword text];
//    NSString *email = [_email text];
//    NSString *mobilePhoneNumber = [_mobilePhoneNumber text];
//    if ([self checkRegisterMessageWithUserName:username withPassword:password withConfirmPassword:confirmPassword] == YES ) {
//        if ([password isEqualToString:confirmPassword]) {
//            [[User sharedInstance] registWithUserName:username withPassword:password withEmail:email withMobilePhoneNumber:mobilePhoneNumber];
//            NSString *message = @"注册成功";
//            [self alterWithMessage:message];
//        }else{
//            NSString *message = @"两次输入的密码不一致";
//            [self alterWithMessage:message];
//        }
//    }
//    if (username.length == 0 || password.length == 0 || confirmPassword.length == 0) {
//        NSString *message = @"请重新输入";
//        [self alterWithMessage:message];
//    }
    
    //创建一个自定义的用户对象
    User *u = [[User alloc]init];
    u.name = self.username.text;
    u.email = self.email.text;
    u.password = self.password.text;
    
    
    
    //创建一个BmobUser对象
    BmobUser *bUser = [[BmobUser alloc]init];
    [bUser setUsername:u.name];
    [bUser setEmail:u.email];
    [bUser setPassword:u.password];
    
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {//如果注册 成功
            
            //用户注册成功后会生成一个 bUser对象，通过[BmobUser getCurrentUser]方法获取该用户对象
            //获取当前注册成功的bUser
            BmobUser *bUser = [BmobUser getCurrentUser];
            
            //应用开启了邮箱验证功能
            if ([bUser objectForKey:@"emailVerified"]) {
                //用户没验证过邮箱
                if (![[bUser objectForKey:@"emailVerified"] boolValue]) {
                    [bUser verifyEmailInBackgroundWithEmailAddress:bUser.email];
                }
            }
            
            
            //打印用户信息
            NSLog(@"username：%@  email:%@",bUser.username,bUser.email);
            
        } else {
            NSLog(@"注册失败-----%@",error);
        }
    }];


}
#pragma mark 弹框
-(void)alterWithMessage:(NSString *)message{
    UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    LoginViewController * vc = [[LoginViewController alloc] init];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [self presentViewController:vc animated:YES completion:nil];
    }];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        if ([message isEqualToString:@"注册成功"]) {
            [self presentViewController:vc animated:YES completion:nil];
        }
    }];
    [alterController addAction:cancleAction];
    [alterController addAction:confirmAction];
    [self.navigationController presentViewController:alterController animated:YES completion:nil];
    
}

#pragma mark 判断注册的条件
-(BOOL)checkRegisterMessageWithUserName:(NSString *)userNmae withPassword:(NSString *)password withConfirmPassword:confirmPassword
{
    if (!([password isEqualToString:@""] && [userNmae isEqualToString: @""] && [confirmPassword isEqualToString: @""])) {
        return YES;
    }
    return NO;
}

@end
